package com.xiumiing.uilibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.DrawableRes;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


public class TitleBarLayout extends LinearLayoutCompat {

    private AttributeSet mAttrs;
    private AppCompatTextView mTitle;
    private TypedArray mTypedArray;
    private ImageView mTitleImg;
    private View mLine;
    private AppCompatTextView mSubTitle;

    public TitleBarLayout(Context context) {
        super(context);
        setUp();
    }

    public TitleBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mAttrs = attrs;
        mTypedArray = getContext().obtainStyledAttributes(mAttrs, R.styleable.TitleBarLayout);
        setUp();
    }

    public TitleBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mAttrs = attrs;
        mTypedArray = getContext().obtainStyledAttributes(mAttrs, R.styleable.TitleBarLayout);
        setUp();
    }


    /**
     * 初始化
     */
    private void setUp() {
        setOrientation(VERTICAL);
        addView(getLLTitle(), 0);
        addView(getLine(), 1);
    }


    /**
     * 公共部分 Title
     *
     * @return
     */
    private LinearLayoutCompat getLLTitle() {
        LinearLayoutCompat mLlTitle = new LinearLayoutCompat(getContext());
        int dimension = (int) mTypedArray.getDimension(R.styleable.TitleBarLayout_title_height, dp2px(35));
        mLlTitle.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dimension));
        mLlTitle.setPadding(dp2px(10), 0, dp2px(15), 0);
        mLlTitle.setOrientation(HORIZONTAL);
        mLlTitle.setGravity(Gravity.CENTER_VERTICAL);
        mLlTitle.addView(createLabel());
        mLlTitle.addView(createTitleImg());
        mLlTitle.addView(createTitle());
        mLlTitle.addView(createSubTitle());
        return mLlTitle;
    }

    /**
     * 公共部分  黄色小标签
     *
     * @return
     */
    private View createLabel() {
        View mLabel = new View(getContext());
        LayoutParams params = new LayoutParams(dp2px(2), dp2px(13));
        params.setMargins(0, 0, dp2px(5), 0);
        mLabel.setLayoutParams(params);
        mLabel.setBackgroundColor(mTypedArray.getColor(R.styleable.TitleBarLayout_label_color, Color.parseColor("#ff4c4c")));
        if (mTypedArray.getBoolean(R.styleable.TitleBarLayout_isShowLabel, true)) {
            mLabel.setVisibility(VISIBLE);
        } else {
            mLabel.setVisibility(GONE);
        }
        return mLabel;
    }

    /**
     * 公共部分  图标
     *
     * @return
     */
    private View createTitleImg() {
        mTitleImg = new ImageView(getContext());
        int titleImgSize = (int) mTypedArray.getDimension(R.styleable.TitleBarLayout_title_img_size, dp2px(15));
        LayoutParams params = new LayoutParams(titleImgSize, titleImgSize);
        params.setMargins(0, 0, dp2px(10), 0);
        mTitleImg.setLayoutParams(params);
        mTitleImg.setBackground(mTypedArray.getDrawable(R.styleable.TitleBarLayout_title_img));
        if (mTypedArray.getBoolean(R.styleable.TitleBarLayout_isShowLabel_title_img, false)) {
            mTitleImg.setVisibility(VISIBLE);
        } else {
            mTitleImg.setVisibility(GONE);
        }
        return mTitleImg;
    }


    /**
     * 公共部分  标题
     *
     * @return
     */
    private AppCompatTextView createTitle() {
        AppCompatTextView mTitle = new AppCompatTextView(getContext());
        LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);
        mTitle.setLayoutParams(params);
        mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTypedArray.getDimension(R.styleable.TitleBarLayout_title_size, sp2px(15)));
        mTitle.setTextColor(mTypedArray.getColor(R.styleable.TitleBarLayout_title_color, Color.parseColor("#333333")));
        mTitle.setText(mTypedArray.getString(R.styleable.TitleBarLayout_title_name));
        this.mTitle = mTitle;
        return this.mTitle;
    }


    /**
     * 副标题  标题
     *
     * @return
     */
    private AppCompatTextView createSubTitle() {
        AppCompatTextView mSubTitle = new AppCompatTextView(getContext());
        LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);
        params.weight = 1;
        mSubTitle.setLayoutParams(params);
        mSubTitle.setGravity(Gravity.END);
        mSubTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTypedArray.getDimension(R.styleable.TitleBarLayout_subtitle_size, sp2px(15)));
        mSubTitle.setTextColor(mTypedArray.getColor(R.styleable.TitleBarLayout_subtitle_color, Color.parseColor("#555555")));
        mSubTitle.setText(mTypedArray.getString(R.styleable.TitleBarLayout_subtitle_name));
        this.mSubTitle = mSubTitle;
        return mSubTitle;
    }

    /**
     * 公共部分  分割线
     *
     * @return
     */
    private View getLine() {
        mLine = new View(getContext());
        LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
        params.setMargins(dp2px(10), 0, 0, 0);
        mLine.setLayoutParams(params);
        mLine.setBackgroundColor(Color.parseColor("#e3e3e3"));
        mLine.setVisibility(mTypedArray.getBoolean(R.styleable.TitleBarLayout_isShow_line, true) ? VISIBLE : GONE);
        return mLine;
    }

    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public int dp2px(final float dpValue) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    private float sp2px(float spValue) {
        final float scale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return spValue * scale;
    }

    public TextView getTitle() {
        return mTitle;
    }

    public TextView getSubTitle() {
        return mSubTitle;
    }

    public ImageView getTitleImg() {
        return mTitleImg;
    }

    public void setTitleImg(ImageView titleImg) {
        mTitleImg = titleImg;
    }

    public void setLine(View line) {
        mLine = line;
    }

    /**
     * 设置 Title
     *
     * @param text
     */
    public void setTitle(CharSequence text) {
        mTitle.setText(text);
    }


    /**
     * 设置 Title
     */
    public void setTitleImg(@DrawableRes int resid) {
        mTitleImg.setBackgroundResource(resid);
    }
}
